package com.xfly.app.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ptmall.app.R;
import com.ptmall.app.utils.GlideUtil;
import com.xfly.app.bean.BankBean;
import com.xfly.app.bean.KcBean;

import java.util.List;


/**
 * Created by Administrator on 2018/1/12.
 */



public class KcAdapter extends BaseAdapter {

    private Context mContext;
    private List<KcBean> mDataList;
    private LayoutInflater mLayoutInflater;

    public KcAdapter(Context context, List<KcBean> list) {
        this.mContext = context;
        this.mDataList = list;
        this.mLayoutInflater = LayoutInflater.from(mContext);
    }

    public void pushData(List<KcBean> list){
        this.mDataList = list;
        notifyDataSetChanged();
    }

    String status = "2";

    public void setStatus(String status){
        this.status = status;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if(mDataList == null){
            return 0;
        }
        return mDataList.size();
    }

    Click click;

    public void setClick(Click click){
        this.click = click;
    }


    @Override
    public Object getItem(int position) {
        return mDataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (null == convertView) {
            convertView = mLayoutInflater.inflate(R.layout.item_kcgood, parent,false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        KcBean bean = mDataList.get(position);

        viewHolder.title.setText(bean.name);

        if(bean.isgift.equals("1")){
            viewHolder.mz.setVisibility(View.VISIBLE);
        }else {
            viewHolder.mz.setVisibility(View.GONE);
        }

        if(bean.isseckill.equals("1")){
            viewHolder.ms.setVisibility(View.VISIBLE);
        }else {
            viewHolder.ms.setVisibility(View.GONE);
        }

        viewHolder.ys.setText("已售："+bean.sale);
        viewHolder.kc.setText("库存："+bean.stock);


        viewHolder.price.setText("￥"+bean.price);

        viewHolder.bj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                click.Click(position);
            }
        });
        viewHolder.xj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                click.Click2(position);
            }
        });

        if(status.equals("2")){
            viewHolder.bj.setVisibility(View.VISIBLE);
            viewHolder.sxj.setText("下架");
            viewHolder.fx.setVisibility(View.VISIBLE);
        }else {
            viewHolder.bj.setVisibility(View.GONE);
            viewHolder.fx.setVisibility(View.GONE);
            viewHolder.sxj.setText("上架");
        }

        GlideUtil.showImg(mContext,bean.cover,viewHolder.logo);

        viewHolder.fx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                click.Click3(position);
            }
        });

        return convertView;
    }

    class ViewHolder {

        TextView title,ms,mz,ys,kc,price,sxj;
        ImageView logo;
        LinearLayout bj,xj,fx;

        ViewHolder(View view) {
            bj = view.findViewById(R.id.bj);
            xj = view.findViewById(R.id.xj);
            sxj = view.findViewById(R.id.sxj);
            fx = view.findViewById(R.id.fx);
            title = (TextView) view.findViewById(R.id.title);
            price = (TextView) view.findViewById(R.id.price);
            ms = (TextView) view.findViewById(R.id.ms);
            mz = (TextView) view.findViewById(R.id.mz);
            ys = (TextView) view.findViewById(R.id.ys);
            kc = (TextView) view.findViewById(R.id.kc);
            logo = (ImageView) view.findViewById(R.id.logo);
        }
    }

    public  interface Click{
        void Click(int position);
        void Click2(int position);
        void Click3(int position);
    }

}
